package com.cxc.news.controller;

import com.cxc.news.pojo.News;
import com.cxc.news.pojo.Type;
import com.cxc.news.service.NewsService;
import com.cxc.news.service.TypeService;
import com.cxc.news.utils.Value;
import com.github.pagehelper.PageInfo;
import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class IndexController {
    @Autowired
    private TypeService typeService;
    @Autowired
    private NewsService newsService;

    /**
     * 主页
     * @param map
     * @return
     */
    @RequestMapping("/index")
    public String toIndex(Map map){
        //根据分类查5条新闻
        Map<String,List<News>> typesNews = new HashMap<String, List<News>>();
        List<Type> types = typeService.getTypeList();
        for (Type t : types){
            List<News> res = newsService.listNewsByTypeIdNoPage(t.getId());
            if (res.size() > 0){
                typesNews.put(t.getName(),res);
            }
        }
        map.put("types",types);
        map.put("typesNews",typesNews);
        //查最热门的5条新闻
        List<News> topNews = newsService.getTopViewsNews(5);
        if (topNews.size() > 0){
            System.out.println("ssss:"+topNews.get(0).getType());
            map.put("topNews",topNews);
        }
        //查最新的5条新闻
        List<News> newsNewss =  newsService.getNewNewsLismitSize(5);
        map.put("newsNewss",newsNewss);
        return Value.PATH_INDEX;
    }

    @GetMapping("/publishs")
    public String toPublish(Map map){
        List<Type> types = typeService.getTypeList();

        map.put("types",types);
        return Value.PATH_PUBLISH;
    }

    /**
     * 修改新闻
     * @param newsId
     * @param map
     * @return
     */
    @GetMapping("/publishs/{newsId}")
    public String toPublishInput(@PathVariable("newsId") Long newsId,
                                 Map map){
        News news = newsService.getNews(newsId);
        if (news != null){
            map.put("news",news);
        }
        List<Type> types = typeService.getTypeList();
        map.put("types",types);
        return Value.PATH_PUBLISH_INPUT;
    }

    /**
     * 分类来显示新闻
     * @param typeId
     * @return
     */
    @GetMapping("/newstype/{typeId}/{pageNum}")
    public String toNewsTypes(@PathVariable("typeId") Long typeId,
                              @PathVariable("pageNum") Integer pageNum,
                              Map map){
        Type type = typeService.getType(typeId);
        List<Type> types = typeService.getTypeList();
        PageInfo<News> pageInfo = newsService.listNewsByTypeId(pageNum,5,typeId);
        if (pageInfo.getTotal() > 0){
            map.put("pages",pageInfo);
        }
        map.put("type",type);
        map.put("types",types);

        return Value.PATH_NEWS_TYPE;
    }

}
